﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace TbShop.Web.Helpers
{
    public class DropdownHelper
    {
        ///<summary>
        ///CodeTable作为数据源的选择项
        ///</summary>
        ///<param name="catagory"></param>
        ///<returns></returns>
        public static IEnumerable<SelectListItem> ConvertToSelectListItem(string category)
        {
            var selectListItems = from c in CodeTableHelper.GetCodeTableDataByCategory(category)
                                  orderby c.OrderNum
                                  select new SelectListItem
                                  {
                                      Text = c.CodeDescription,
                                      Value = c.Code
                                  };
            return selectListItems;
        }

        ///<summary>
        ///自定义数据源{Key,Value}
        ///</summary>
        ///<param name="category"></param>
        ///<param name="dataSource"></param>
        ///<returns></returns>
        public static IEnumerable<SelectListItem> ConvertToSelectListItem(IEnumerable<dynamic> dataSource, bool needInsertEmptyRow = false)
        {
            var selectListItems = (from c in dataSource
                                   select new SelectListItem
                                   {
                                       Text = c.Value,
                                       Value = c.Key.ToString()
                                   }).ToList();
            if (needInsertEmptyRow)
            {
                selectListItems.Insert(0, new SelectListItem() { Text = "请选择", Value = string.Empty });
            }
            return selectListItems;
        }

        public class SelectListItemSource
        {
            public string Value { set; get; }

            public string Key { set; get; }
        }
    }
}